package org.jeecg.modules.hotel.history.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecg.modules.hotel.history.entity.LwgyHotelHistory;
import org.jeecg.modules.hotel.history.service.ILwgyHotelHistoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 住宿历史记录
 * @Author: jeecg-boot
 * @Date:   2021-02-24
 * @Version: V1.0
 */
@Api(tags="住宿历史记录")
@RestController
@RequestMapping("/hotel/lwgyHotelHistory")
@Slf4j
public class LwgyHotelHistoryController extends JeecgController<LwgyHotelHistory, ILwgyHotelHistoryService> {
	@Autowired
	private ILwgyHotelHistoryService lwgyHotelHistoryService;

	@Autowired
	private ILwgyBaseStudentService lwgyBaseStudentService;

	 /**
      * 分页列表查询
      *
      * @param lwgyHotelHistory
      * @param pageNo
      * @param pageSize
      * @param req
      * @return
      */
	@AutoLog(value = "住宿历史记录-分页列表查询")
	@ApiOperation(value="住宿历史记录-分页列表查询", notes="住宿历史记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyHotelHistory lwgyHotelHistory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyHotelHistory> queryWrapper = QueryGenerator.initQueryWrapper(lwgyHotelHistory, req.getParameterMap());
		Page<LwgyHotelHistory> page = new Page<LwgyHotelHistory>(pageNo, pageSize);
		String studentNumber = req.getParameter("studentNumber");
		if(studentNumber != null && !"".equals(studentNumber)){
			String stuId = lwgyBaseStudentService.getStuIdByNumber(studentNumber);
			if(null != stuId && !"".equals(stuId)){
				queryWrapper.eq("person_id",stuId);
			}
		}
		IPage<LwgyHotelHistory> pageList = lwgyHotelHistoryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param lwgyHotelHistory
	 * @return
	 */
	@AutoLog(value = "住宿历史记录-添加")
	@ApiOperation(value="住宿历史记录-添加", notes="住宿历史记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyHotelHistory lwgyHotelHistory) {
		lwgyHotelHistoryService.save(lwgyHotelHistory);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param lwgyHotelHistory
	 * @return
	 */
	@AutoLog(value = "住宿历史记录-编辑")
	@ApiOperation(value="住宿历史记录-编辑", notes="住宿历史记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyHotelHistory lwgyHotelHistory) {
		lwgyHotelHistoryService.updateById(lwgyHotelHistory);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "住宿历史记录-通过id删除")
	@ApiOperation(value="住宿历史记录-通过id删除", notes="住宿历史记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyHotelHistoryService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "住宿历史记录-批量删除")
	@ApiOperation(value="住宿历史记录-批量删除", notes="住宿历史记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyHotelHistoryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "住宿历史记录-通过id查询")
	@ApiOperation(value="住宿历史记录-通过id查询", notes="住宿历史记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyHotelHistory lwgyHotelHistory = lwgyHotelHistoryService.getById(id);
		if(lwgyHotelHistory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyHotelHistory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyHotelHistory
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyHotelHistory lwgyHotelHistory) {
		String title="住宿历史记录";
		// Step.1 组装查询条件
		QueryWrapper<LwgyHotelHistory> queryWrapper = QueryGenerator.initQueryWrapper(lwgyHotelHistory, request.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		List<LwgyHotelHistory> exportList = lwgyHotelHistoryService.list( queryWrapper);
		// Step.2	遍历查询结果列表，处理每个实体对象
		for (LwgyHotelHistory entity : exportList) {
			// Step.3 获取需要转换的字段值，并根据字段值进行相应的转换
			String behaveValue = entity.getBehavior();
			String behaveText = getBehaveText(behaveValue);

			// Step.4 将转换后的文本值设置到对应的属性中
			entity.setBehavior(behaveText);
		}
		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS,  LwgyHotelHistory.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;


    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyHotelHistory.class);
    }

	 private String getBehaveText(String behaveValue) {
		 String behaveText;
		 switch (behaveValue) {
			 case "choose_bed":
				 behaveText = "入住";
				 break;
			 case "change_bed":
				 behaveText = "调宿";
				 break;
			 case "leave_bed":
				 behaveText = "退宿";
				 break;
			 default:
				 throw new IllegalStateException("Unexpected value: " + behaveValue);
		 }
		 return behaveText;
	 }
}
